#pragma once
#include<iostream>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<string>
#include<strings.h>

class InetAddr
{
public:
    InetAddr(uint16_t port,const std::string&ip="0.0.0.0")
    {
        bzero(&_addr,sizeof(_addr));
        _addr.sin_family=AF_INET;
        _addr.sin_port=htons(_port);
        inet_pton(AF_INET,_ip.c_str(),&(_addr.sin_addr.s_addr));
    }
    InetAddr(struct sockaddr_in&addr)
        :_addr(addr)
    {
        _port=ntohs(addr.sin_port);
        char buffer[1024];
        inet_ntop(AF_INET,&(_addr.sin_addr.s_addr),buffer,sizeof(buffer));
        _ip=buffer;
    }
    struct sockaddr*Addr()
    {
        return (sockaddr*)&_addr;
    }
    socklen_t Len()
    {
        return sizeof(_addr);
    }
    std::string ToString()
    {
        return _ip + "-" + std::to_string(_port);
    }
    ~InetAddr(){}
private:
    struct sockaddr_in _addr;
    uint16_t _port;
    std::string _ip;
};